import { baseUrl } from "./env";
import { ElMessage } from "element-plus";
import axios from "axios";

const service = axios.create({
  baseURL: baseUrl, // api的base_url
  timeout: 5000, // 请求超时时间
});

//添加请求拦截器
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    ElMessage({
      message:  error.message,
      type: "error",
    });
    return Promise.reject(error);
  }
);

//添加响应拦截器
service.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return response;
    } else {
      ElMessage({
        message: response.statusText,
        type: "success",
      });
      return Promise.reject(response.statusText);
    }
  },
  (error) => {
    ElMessage({
      message: error.message,
      type: "error",
    });
    return Promise.reject(error);
  }
);

export default service;
